/**
 * Created by wenquxing on 2016/5/20.
 */
var usr=require('../db/db');
var uuid = require('node-uuid'); //调用uuid模块

var client=usr.connect();

//根据用户名查找用户信息
function checkuser(username ,callback){

    client.query('select t.name,t.password from t_user t where t.name= ?',[username],function(err,results,fields){
      if(err) throw err;
            callback(results);

    });
}

//新增用户信息
function Insuser(username ,password,callback){

    client.query('INSERT INTO t_user(id,name,password) VALUES(?,?,?)',[uuid.v4(),username,password],function(err,results,fields){
        if( err ){
          console.log( "error:" + err.message);
           return err;
        }
       callback(err);
    });
}

//根据用户名密码查找用户信息
function login(username ,password,callback){

    client.query('select t.id,t.name,t.`password` from t_user t where t.`name`=? and t.`password`= ?  ',[username,password],function(err,results,fields){
        if(err) throw err;
        var user={
            id:results[0].id,
            name:results[0].name,
            password :results[0].password
        }
        callback(user);
    });
}

//根据用户名和密码查询是否匹配
function checkpassword(username ,password,callback){

    client.query('select count(1) from t_user t where t.`name`=?  and t.`password`= ? ',[username,password],function(err,results,fields){
        if(err) throw err;
        callback(results);
    });
}

//根据用户名和密码查询是否匹配
function getUser(username ,callback){

    client.query('select t.id,t.name from t_user t where t.`name`=? ',[username],function(err,results,fields){
        if(err) throw err;
        if(results==0){
            callback(user);
        }else{
            var user={
                id:results[0].id,
                name:results[0].name
            }
            callback(user);
        }


    });
}


exports.checkuser  = checkuser;
exports.Insuser  = Insuser;
exports.login = login;
exports.checkpassword = checkpassword;
exports.getUser = getUser;